<?php
/**
 * Created by PhpStorm.
 * User: CJJ
 * Date: 2017/8/24
 * Time: 8:44
 */

//部门方法类
$Dir=explode('\\',dirname(__FILE__));$CDir=$Dir[0];for($i=1;$i<count($Dir)-3;$i++) $CDir.='/'.$Dir[$i];
//require_once ($CDir.'/GongAn/SQLBase.php');
require_once ('../../SQLBase.php');
require_once ('../../Common.php');



class SectionManager{
    function getAllSectionData() { // 获取表内容
        $db = new SQLBase ();
        $queryStr = 'SELECT * FROM allsection WHERE section_state=1;';
        // 	echo $queryStr;
        $result = $db->queryData ( $queryStr );
        if (! $result||0==$result->num_rows) {
            $result_array ['state'] = - 1; // -1表示数据库出错
        } else {
            $result_array ['state'] = 1;
            while ( !!$temp = $result->fetch_array () ) {
                $result_array ['data'][] = array (
                    section_ID => $temp ['section_ID'],
                    section_name => $temp ['section_name'],
                    section_type=>$temp ['section_type']
                );
            }
        }
        return $result_array;
    }
    /*
     * 获取ALLsection数据量
     */
    function getAllSectionPageCount(){
        $db=new SQLBase();
        $queryStr="SELECT count(*) FROM allsection WHERE section_state=1;";
        // 	echo $queryStr;
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $resultArrayAll['state']=1;
            $temp=$result->fetch_assoc();
            $resultArrayAll['data']=$temp['count(*)'];
        }
        return $resultArrayAll;
    }
    /*
     * 获取ALLsection部门 分页显示
     */
    function getSectionDataLimit($n,$m){
        $db = new SQLBase ();
        $queryStr = 'SELECT * FROM allsection WHERE section_state=1 LIMIT '.$n.','.$m.';';

        $result = $db->queryData ( $queryStr );
        if (! $result||0==$result->num_rows) {
            $result_array ['state'] = - 1; // -1表示数据库出错
        } else {
            $result_array ['state'] = 1;
            while ( !!$temp = $result->fetch_array () ) {
                $result_array ['data'][] = array (
                    section_ID => $temp ['section_ID'],
                    section_name => $temp ['section_name'],
                    section_type=>$temp ['section_type']
                );
            }
        }
        return $result_array;
    }
    /*
     * 获取部门员工数量
     */
    function getOneSectionPageCount($sectionId){
        $db=new SQLBase();
        $queryStr="SELECT count(*) FROM section WHERE section_ID=".$sectionId.";";
        // 	echo $queryStr;
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $resultArrayAll['state']=1;
            $temp=$result->fetch_assoc();
            $resultArrayAll['data']=$temp['count(*)'];
        }
        return $resultArrayAll;
    }

    /*
     * 添加部门
     */
    function insertAllSection($sectionName,$sectionType){
        $db = new SQLBase ();
        $db->queryData('SET AUTOCOMMIT = 0');
        $db->queryData('BEGIN');
        $queryStr = 'INSERT INTO allsection(section_name,section_type) VALUES("'.$sectionName.'",'.$sectionType.');';
        // 		echo $queryStr;
        $result_array = array();
        $result = $db->queryData ( $queryStr );
        if (! $result) {
            $result_array ['state'] = - 1; // -1表示数据库出错
        } else {
           $last = $db->getLastInserID();
           if($last == 0){
               $result_array['state'] == -1;
           }
           else{
               $query = "INSERT INTO section_fee(section_ID) VALUES('$last')";
               $r = $db->queryData($query);
               if($r){
                   $result_array['state'] = 1;
               }
               else{
                   $result_array['state'] = -1;
               }
           }
        }
        if($result_array['state'] == 1){
            $db->queryData('COMMIT');
        }
        else{
            $db->queryData('ROLLBACK');
        }
        $db->queryData('SET AUTOCOMMIT = 1');
        return $result_array;
    }
    /*
     * 通过部门名称获取ALLsection获取一个部门的信息
     */
    function getOneAllSectionBySectionName($sectionName){
        $db = new SQLBase ();
        $queryStr = 'SELECT * FROM allsection WHERE section_name="'.$sectionName.'";';
        // 	 		echo $queryStr;
        $result = $db->queryData ( $queryStr );
        if (! $result||0==$result->num_rows) {
            $result_array ['state'] = 1; // -1表示数据库出错
        } else {
            $result_array ['state'] = -1;
        }
        return $result_array;
    }
    /*
     * 获取部门用户信息
     */
    function getSectionUserData($sectionId,$n,$m){
        $db = new SQLBase ();
        $queryStr = 'SELECT section.user_ID,`user`.user_name,section.section_job FROM section,user where section.user_ID=user.user_ID AND section.section_ID='.$sectionId.' LIMIT '.$n.','.$m.';';
        //  		echo $queryStr;
        $result = $db->queryData ( $queryStr );
        if (! $result||0==$result->num_rows) {
            $result_array ['state'] = - 1; // -1表示数据库出错
        } else {
            $result_array ['state'] = 1;
            while ( !!$temp = $result->fetch_array () ) {
                $result_array ['data'][] = array (
                    user_ID => $temp ['user_ID'],
                    user_name => $temp ['user_name'],
                    section_job => $temp ['section_job']
                );
            }
        }
        return $result_array;
    }
    /*
     * 获取一个部门的基本信息
     */
    function getSectionData($sectionId){
        $db = new SQLBase ();
        $queryStr = 'SELECT * FROM allsection WHERE section_ID='.$sectionId.';';
        //  		echo $queryStr;
        $result = $db->queryData ( $queryStr );
        if (! $result||0==$result->num_rows) {
            $result_array ['state'] = - 1; // -1表示数据库出错
        } else {
            $result_array ['state'] = 1;
            while ( !!$temp = $result->fetch_array () ) {
                $result_array ['data'][] = array (
                    section_ID => $temp ['section_ID'],
                    section_name => $temp ['section_name']
                );
            }
        }
        return $result_array;
    }
    /*
     * 更新部门基本信息
     */
    function updateSectionData($data){
        $db = new SQLBase ();
        $queryStr = 'UPDATE allsection SET section_name="'.$data['section_name'].'" WHERE section_ID='.$data['section_ID'].';';
        //echo $queryStr;
        $result = $db->queryData ( $queryStr );
        if (!$result) {
            $result_array ['state'] = - 1; // -1表示数据库出错
        } else {
            $result_array ['state'] = 1;
        }
        return $result_array;
    }
    /*
     * deleteSection
     * 删除部门
     */
    function  deleteSectionData($sectonId){
        $db=new SQLBase();
        $queryStr = 'UPDATE allsection SET section_state=0 WHERE section_ID='.$sectonId.';';
        $result = $db->queryData ( $queryStr );
        //echo $queryStr;
        if (!$result) {
            $result_array ['state'] = - 1; // -1表示数据库出错
        } else {
            $queryStr='DELETE FROM section  WHERE section_ID='.$sectonId.';';
            //echo $queryStr;
            $result = $db->queryData ( $queryStr );
            if (! $result) {
                $result_array ['state'] = - 1; // -1表示数据库出错
            }
            else{
                $result_array ['state'] = 1;
            }
        }
        return $result_array;
    }
    /*
     * 获取用户信息通过名字
     */
    function getSectionDataByName($user_name,$section_ID){
        $db = new SQLBase ();
        $queryStr = 'SELECT section.user_ID,`user`.user_name,section.section_job FROM section,user where section.user_ID=user.user_ID AND section_ID='.$section_ID.' AND user.user_name LIKE ("%%'.$user_name.'%%");';
        //echo $queryStr;
        $result = $db->queryData ( $queryStr );
        if (! $result||0==$result->num_rows) {
            $result_array ['state'] = - 1; // -1表示数据库出错
        } else {
            $result_array ['state'] = 1;
            while ( !!$temp = $result->fetch_array () ) {
                $result_array ['data'][] = array (
                    user_ID => $temp ['user_ID'],
                    user_name => $temp ['user_name'],
                    section_job => $temp ['section_job']
                );
            }
        }
        return $result_array;
    }
    /*
     * 删除部门员工
     */
    function deleteSectionUserById($user_ID,$section_ID){
        $db = new SQLBase ();
        $queryStr = "DELETE FROM section WHERE user_ID=".$user_ID.' AND section_ID='.$section_ID.';';
//        echo $queryStr;
        $result = $db->queryData ( $queryStr );
        if (! $result) {
            $result_array ['state'] = - 1; // -1表示数据库出错
        } else {
            $result_array ['state'] = 1;
        }
        return $result_array;
    }

    /*
     * 获取数据数量
     */
    function getUserCount($sectionId){
        $db=new SQLBase();
        $queryStr="SELECT COUNT(*) FROM `user` WHERE user_ID NOT IN(SELECT user_ID FROM section WHERE section_ID=".$sectionId.");";
        $result=$db->queryData($queryStr);
        if (! $result) {
            $result_array ['state'] = - 1; // -1表示数据库出错
        } else {
            $result_array ['state'] = 1;
            $temp = $result->fetch_array ();
            $result_array['data']=$temp['COUNT(*)'];
        }
        return $result_array;
    }
    /*
     * 获取分页用户数据
     */
    function getUserDataUsePage($curpage,$sectionId){
        $db=new SQLBase();
        $queryStr='SELECT * from `user`  WHERE user_ID NOT IN (SELECT user_ID FROM section WHERE section_ID='.$sectionId.') LIMIT '.($curpage*10).',10;';
        //echo $queryStr;
        $result=$db->queryData($queryStr);
        if (! $result) {
            $result_array ['state'] = - 1; // -1表示数据库出错
        } else {
            $result_array ['state'] = 1;
            while($temp = $result->fetch_array ()){
                $result_array['data'][]=array(
                    user_ID=>$temp['user_ID'],
                    user_number=>$temp['user_ID'],
                    user_name=>$temp['user_name']
                );
            }
        }
        return $result_array;
    }
    /*
     * 搜索用户信息通过用户姓名
     */
    function AddSearchUserDataByName($user_name,$sectionId){
        $db=new SQLBase();
        $queryStr='SELECT * from `user`  WHERE user_name LIKE "%%'.$user_name.'%%" AND user_ID NOT IN (SELECT user_ID FROM section WHERE section_ID='.$sectionId.');';
//        echo $queryStr;
        $result=$db->queryData($queryStr);
        if (! $result) {
            $result_array ['state'] = - 1; // -1表示数据库出错
        } else {
            $result_array ['state'] = 1;
            while($temp = $result->fetch_array ()){
                $result_array['data'][]=array(
                    user_ID=>$temp['user_ID'],
                    user_number=>$temp['user_ID'],
                    user_name=>$temp['user_name']
                );
            }
        }
        return $result_array;
    }
    function SearchUserDataByName($user_name,$sectionId){
        $db=new SQLBase();
        $queryStr="SELECT * FROM `user`,section WHERE section.user_ID=user.user_ID AND user.user_name LIKE ('%%".$user_name."%%') HAVING user.user_ID  IN(SELECT user_ID FROM section WHERE section_ID=".$sectionId.")";
//        echo $queryStr;
        $result=$db->queryData($queryStr);
        //echo $queryStr;
        if (! $result) {
            $result_array ['state'] = - 1; // -1表示数据库出错
        } else {
            $result_array ['state'] = 1;
            while($temp = $result->fetch_array ()){
                $result_array['data'][]=array(
                    user_ID=>$temp['user_ID'],
                    section_job=>$temp['section_job'],
                    user_name=>$temp['user_name']
                );
            }
        }
        return $result_array;
    }
    /*
     * 添加用户到部门
     */
    function addToSection($data){
        $db = new SQLBase ();
        $queryStr = 'INSERT INTO section(user_ID,section_ID,section_job) VALUES('.$data['user_ID'].','.$data['section_ID'].',"'.$data['section_job'].'");';
        //echo $queryStr;
        $result = $db->queryData ( $queryStr );
        if (! $result) {
            $result_array ['state'] = - 1; // -1表示数据库出错
        } else {
            $result_array ['state'] = 1;
        }
        return $result_array;
    }
}

?>